package controller

import (
	"gin/common/lib"
	"gin/middleware"
	"gin/model/body"
	"gin/service/newsService"
	"gin/service/newsService/newsCommentService"
	"gin/service/newsService/newsLikeService"
	"gin/util/jwt"
	"gin/util/map_builder"
	"gin/util/request"
	"github.com/gin-gonic/gin"
)

type NewsController struct {
}

func NewsRegister(router lib.RegisterRouterGroup, needLoginRouter lib.RegisterRouterGroup) {
	news := NewsController{}
	needLoginRouter.POST("/news/add", news.add)
	needLoginRouter.POST("/news/update", news.update)
	needLoginRouter.POST("/news/list", news.list)
	needLoginRouter.POST("/news/del", news.del)
	needLoginRouter.POST("/news/detail", news.detail)
	needLoginRouter.POST("/news/addLike", news.addLike)
	needLoginRouter.POST("/news/addComment", news.addComment)
	needLoginRouter.POST("/news/delComment", news.delComment)
	needLoginRouter.POST("/news/listComment", news.listComment)
	needLoginRouter.POST("/news/unreadNum", news.unreadNum)
	needLoginRouter.POST("/news/unreadList", news.unreadList)
	needLoginRouter.POST("/news/read", news.read)
}

// @Summary 发布动态
func (*NewsController) add(c *gin.Context) {
	var p body.AddNewsParam
	request.Bind(c, &p)

	p.Uid = jwt.GetUid(c, true)
	newsService.Add(p)

	middleware.Success(c)
}

// @Summary 修改动态
func (*NewsController) update(c *gin.Context) {
	var p body.UpdateNewsParam
	request.Bind(c, &p)

	newsService.Update(p)
	middleware.Success(c)
}

// @Summary 获取动态列表
func (*NewsController) list(c *gin.Context) {
	var p body.NewsListParam
	request.Bind(c, &p)

	uid := jwt.GetUid(c, true)
	list, count := newsService.List(uid, p)
	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 删除动态
func (*NewsController) del(c *gin.Context) {
	var p body.IdParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	newsService.Del(uid, p.Id)
	middleware.Success(c)
}

// @Summary 获取动态详情
func (*NewsController) detail(c *gin.Context) {
	var p body.IdParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)
	info := newsService.Detail(p.Id, uid)
	middleware.Success(c, map_builder.BuilderMap("info", info))
}

// @Summary 点赞动态
func (*NewsController) addLike(c *gin.Context) {
	var p body.IdParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	newsLikeService.Add(uid, p.Id)
	middleware.Success(c)
}

// @Summary 评论动态
func (*NewsController) addComment(c *gin.Context) {
	var p body.NewsCommentParam
	request.Bind(c, &p)
	p.Uid = jwt.GetUid(c, true)

	newsCommentService.AddComment(p)
	middleware.Success(c)
}

// @Summary 删除评论
func (*NewsController) delComment(c *gin.Context) {
	var p body.DelNewsParam
	request.Bind(c, &p)

	newsCommentService.DelComment(p)
	middleware.Success(c)
}

// @Summary 获取评论列表
func (*NewsController) listComment(c *gin.Context) {
	var p body.NewsCommentListParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	list := newsCommentService.List(uid, p)
	middleware.Success(c, map_builder.BuilderMap("list", list))
}

// @Summary 获取未读评论数量
func (*NewsController) unreadNum(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	num := newsCommentService.UnreadNum(uid)
	middleware.Success(c, map_builder.BuilderMap("num", num))
}

// @Summary 未读评论列表
func (*NewsController) unreadList(c *gin.Context) {
	var p body.PageTrait
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)
	list, count := newsCommentService.UnreadList(uid, p)
	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 评论已读
func (*NewsController) read(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	newsCommentService.Read(uid)
	middleware.Success(c)
}
